// merchants/holderList/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    holderList: [],
    listType: 1,//1股东列表，2发放量，3核销量
    gradeid: 0,
    checkList: []
  },

  // 股东列表
  getShareholderList: function (gradeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetShareholderList?createUid=' + app.globalData.userInfo.Uid + '&gradeId=' + gradeid + '&page=' + that.data.CurrentPage,
      success: function (res) {
        if (that.data.listType == 1 && gradeid == 0) {
          wx.setNavigationBarTitle({
            title: '所有股东（' + res.data.Data.PageModel.TotalCount + '）',
          })
        }
        if (res.data.Code == 0) {
          var holderList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.holderList
            holderList = preGoods.concat(holderList);
          }
          that.setData({
            holderList: holderList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 股东礼包发放记录
  getPackageRecardList: function (gradeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetPackageRecardList?creatUid=' + app.globalData.userInfo.Uid + '&gradeId=' + gradeid + '&page=' + that.data.CurrentPage,
      success: function (res) {
        if (res.data.Code == 0) {
          let holderList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.holderList
            holderList = preGoods.concat(holderList);
          }
          for (let i = 0; i < holderList.length; i++) {
            holderList[i].CreateTime = holderList[i].CreateTime.substr(0, 10);
          }
          that.setData({
            holderList: holderList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 核销记录
  getCheckList: function (gradeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetCheckList?createUid=' + app.globalData.userInfo.Uid + '&gradeId=' + gradeid + '&page=' + that.data.CurrentPage,
      success: function (res) {
        if (that.data.listType == 3) {
          wx.setNavigationBarTitle({
            title: '核销量（' + res.data.Data.PageModel.TotalCount + '）',
          })
        }
        if (res.data.Code == 0) {
          let checkList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.checkList;
            checkList = preGoods.concat(checkList);
          }
          for (let i = 0; i < checkList.length; i++) {
            checkList[i].CreateTime = checkList[i].CreateTime.substr(0, 10);
            
          }
          that.setData({
            checkList: checkList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  
  //  获取clientid
  getClientid: function (uid) {
    let cardId = wx.getStorageSync('cardId');
    wx.request({
      url: app.globalData.host + '/api/AddressBook/GetClientInfo',
      data: {
        uid: uid,
        staffId: cardId
      },
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          let urlparam = 'staffId=' + cardId + '&clientid=' + res.data.Data.Id + '&rand=' + Math.random();
          wx.navigateTo({
            url: '/pages/h5page/index?url=customerDetail.html&urlparam=' + encodeURIComponent(urlparam)
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 跳转客户详情页
  toCusDetail: function (e) {
    let shareUid = e.currentTarget.dataset.shareuid;
    let clientid = this.getClientid(shareUid);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.listtype) {
      this.setData({
        listType: options.listtype,
        gradeid: options.gradeid
      })
      if (options.listtype == 1) {
        this.getShareholderList(options.gradeid);
      }
      if (options.listtype == 1 && options.gradeid > 0) {
        wx.setNavigationBarTitle({
          title: options.name + '（' + options.count + ')',
        })
      }
      if (options.listtype == 2) {
        this.getPackageRecardList(options.gradeid);
        wx.setNavigationBarTitle({
          title: '发放量（' + options.count + ')',
        })
      }
      if (options.listtype == 3) {
        this.getCheckList(options.gradeid);
      }

    }
  },

  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      if (that.data.listType == 1) {
        that.getShareholderList(that.data.gradeid);
      }
      if (that.data.listType == 2) {
        that.getPackageRecardList(that.data.gradeid);
      }
      if (that.data.listType == 3) {
        that.getCheckList(that.data.gradeid);
      }
    } else {
      return
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})